/* search */
if(document.getElementById("q").value == "")
	document.getElementById("q").setAttribute('style', 'background: url(/eclipse/chashop/app/common/img/txt-searchks.gif) 10px center no-repeat !important');

document.getElementById("q").onblur = function(){
	if (document.getElementById("q").value == '')
		document.getElementById("q").setAttribute('style', 'background: url(/eclipse/chashop/app/common/img/txt-searchks.gif) 10px center no-repeat !important');
};
document.getElementById("q").onfocus = function(){
	document.getElementById("q").setAttribute('style', 'background: none !important');
};


$(document).ready(function(){

	/* dropdown */
	$("#network").click(function () {
		if ($("div#networkdropdown").is(":hidden")) {
			$("#networkdropdown").fadeIn('fast');
			$("#network").toggleClass("active");
		}
		else {
			$("div#networkdropdown").fadeOut('fast');
			$("#network").toggleClass("active"); }
	});
	$("div#networkdropdown").hover( function() {},
		function() {
			$("div#networkdropdown").fadeOut('fast');
			$("#network").toggleClass("active");
	});

	/* go to top */
	$('a[href=#top]').click(function(){
        $('html, body').animate({scrollTop:0}, 'slow');
        return false;
    });

	/* faq */
	$("dl#faq > dd").hide();
    $("dl#faq > dt").click(function () {
        $(this).next('dd').slideToggle('fast');
        $(this).toggleClass('active');
    });

    /* testimonials */
    var listHeight = $("#testimonials li.active").height();
	$("#testimonials ul").height(listHeight);

	$("#nexttestimonial").click(function() {
		var testimonials  = $("#testimonials ul").children();
		var i = getActiveTestimonial(testimonials);
		setNextTestimonial(i , testimonials);
		return false;
	});

	function getActiveTestimonial(testimonials) {
		for( var i = 0 ; i < testimonials.length ; i++)	{
			if (testimonials.get(i).className == "active")
				return i;
		}
		return false;
	}

	function setNextTestimonial(index , testimonials) {
		var listHeight = $("#testimonials ul").height();
		$("#testimonials ul").height(listHeight+10);
		$(testimonials.get(index)).removeClass('active');

		if ( (index + 1) < testimonials.length) {
			// show next testimonial
			$(testimonials.get(index)).fadeOut(300);
			$(testimonials.get(index + 1)).fadeIn('slow');
			$(testimonials.get(index + 1)).addClass('active');

			var newHeight = $(testimonials.get(index + 1)).height();
			$("#testimonials ul").animate({
				height: newHeight
			});
		}
		else {
			// show first testimonial
			$(testimonials.get(index)).fadeOut(300);
			$(testimonials.get(0)).fadeIn('slow');
			$(testimonials.get(0)).addClass('active');

			var newHeight = $(testimonials.get(0)).height();
			$("#testimonials ul").animate({
				height: newHeight
			});
		}

	}

	/* home page slideshow */
	$('#slides').cycle({
		fx:     'scrollHorz',
		speed:   1200,
		timeout: 9000,
		next:   '#arrowLeft',
		prev:   '#arrowRight',
		pause:   1,
		containerResize: 0,
		fit:0,
        height:	 242,
		cleartypeNoBg: true
	});

    /* sidebar */
	$('#forumwrapper').load("/ks-includes/getforum.php");
	$('#magazinewrapper').load("/ks-includes/getmagazine.php");

	/* social widget */
	$('#tab-subscribe').click(function(event) {
		event.preventDefault();

		if (!$(this).hasClass('active'))
		{
			$(this).toggleClass('active');
			$('#tab-rss').toggleClass('active');

			var emailHeight = $('#widget-email').outerHeight(); // get email height
			var rssHeight = $('#widget-rss').outerHeight(); // get rss height

			$('#widget-rss').fadeOut(300);
			$('#widget-email').fadeIn('slow');
			$("#widget-social").animate({
				height: emailHeight
			});
		}
	});

	$('#tab-rss').click(function(event) {
		event.preventDefault();

		if (!$(this).hasClass('active'))
		{
			$(this).toggleClass('active');
			$('#tab-subscribe').toggleClass('active');

			var emailHeight = $('#widget-email').outerHeight(); // get email height
			var rssHeight = $('#widget-rss').outerHeight(); // get rss height

			$('#widget-email').fadeOut(300);
			$('#widget-rss').fadeIn('slow');
			$("#widget-social").animate({
				height: rssHeight
			});
		}
	});

});
